﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DocuServe.Data
{
    public class RepositoryScope : IDisposable
    {
        protected RepositoryScope() { }

        protected bool HasOpenSession { get; set; }

        public void Commit()
        {
            if (this.HasOpenSession)
                SessionManager.Commit();
        }

        public void Dispose()
        {
            this.Rollback();
            if (this.HasOpenSession)
                SessionManager.Close();
        }

        public void Evict(object obj)
        {
            SessionManager.Evict(obj);
        }

        public void Flush()
        {
            SessionManager.Flush();
        }

        public void Rollback()
        {
            if (this.HasOpenSession)
                SessionManager.Rollback();
        }

        public static RepositoryScope Start()
        {
            RepositoryScope scope = new RepositoryScope();
            if (!SessionManager.HasOpenSession())
            {
                SessionManager.Begin();
                scope.HasOpenSession = true;
            }

            return scope;
        }
    }
}
